package com.two.screen.water.balance.ui.activity

import android.content.Intent
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.two.screen.water.balance.R
import com.two.screen.water.balance.bean.BaseBean
import com.two.screen.water.balance.bean.water.DeviceInfoBean
import com.two.screen.water.balance.net.api.DataApi
import com.two.screen.water.balance.net.api.NormalApi24
import com.two.screen.water.balance.net.observer.DialogObserver
import com.two.screen.water.balance.ui.App
import com.two.screen.water.balance.ui.activity.base.BaseActivity
import com.two.screen.water.balance.ui.service.AppService
import com.two.screen.water.balance.utils.*
import kotlinx.android.synthetic.main.activty_start_use.*


@Route(path = ARouterUtils.StartUseActivity)
class StartUseActivity : BaseActivity() {
    val TAG = StartUseActivity::class.java.simpleName.plus("=>")
    private var mIsAllowed = false
    override fun getContentLayout(): Any = R.layout.activty_start_use
    override fun beforeSetContentView() {
        App.flag = 0
    }

    override fun initView(savedInstanceState: Bundle?) {
        ARouter.getInstance().inject(this)
    }

    override fun initData() {
        //加密 uuid
        var mUniqueId = CommUtils.getAppUUid()
        mDeviceCodeQrIv.setImageBitmap(ZXingUtils.createQRCodeBitmap(mUniqueId, 2000))

        getData()
    }

    private fun getData(isGo2Index: Boolean = false) {
        NormalApi24.getDeviceInfo()
            .compose(RxUtils.aLifecycle(this))
            .subscribe(object : DialogObserver<DeviceInfoBean>(this) {
                override fun onSuccessNext(t: BaseBean<out DeviceInfoBean>) {
                    val deviceInfo = t.data?.deviceInfo
                    DataApi.mMerchantServiceFee = t.data?.bazaar?.merchantServiceFee
                    DataApi.mBuyServiceFee = t.data?.bazaar?.buyServiceFee
                    if (deviceInfo?.status != 0 || deviceInfo.deviceType == null || deviceInfo.deviceType.isNullOrBlank()) {
                        ToastUtil.showToast("未授权!")
                        mTipsTv.text = "未授权"
                        mIsAllowed = false
                    } else {
                        val isPublic = "public" == deviceInfo.deviceType
                        SPUtils.putBoolean(Params.isPublic, isPublic)
                        mTipsTv.text = if (isPublic) "公秤" else "私秤"
                        mIsAllowed = true
                        if (isGo2Index) go2Index()
                    }
                }
            })
    }

    override fun initListener() {
        mStartUseTv.setOnClickListener {
            go2Index() //测试
//            if (mIsAllowed) {
//                go2Index()
//            } else {
//                getData(true)
//            }
//        }
//        mSystemSettingTv.setOnClickListener {
//            ARouter.getInstance()
//                .build(ARouterUtils.SystemSettingActivity24)
//                .navigation()
        }

    }

    private fun go2Index() {
        ARouter.getInstance()
            .build(ARouterUtils.IndexActivity24)
            .navigation()
        finish()
    }

    override fun doubleQuit(): Boolean = true
    override fun exit(): Boolean {
        try {
            if (isExit) {
                stopService(Intent(this, AppService::class.java))
            }
        } catch (e: Exception) {
            d("退出失败:$e")
        }
        return super.exit()
    }
}